import originAxios from 'axios'
import { IAccount, AccountType, AxiosRespond, ICategory, DateFilterType } from '../interface';

const axios = originAxios.create({
    baseURL: process.env.NODE_ENV === 'development' ? '/api': '/'
});

// 获取账目分类
export const getCategoryAPI = () => axios.get<AxiosRespond<ICategory[]>>('/category/get');
// 添加账目分类
export const addCategoryAPI = (category_name: string) => axios.post<AxiosRespond<null>>('/category/add', { category_name });
// 删除账目分类
export const deleteCategoryAPI = (category_id: number) => axios.post<AxiosRespond<null>>('/category/delete', { category_id });


interface IAccountRespond {
    accounts: IAccount[]
    total: number
}
// 获取所有账目,limit分页
export const getAllAccountAPI = (limit: number) => axios.get<AxiosRespond<IAccountRespond>>(`/account/getall?limit=${limit}`);
// 根据月份或者年份获取账目
export const getAccountByDateAPI = (date_time: Date, type: DateFilterType) => axios.get<AxiosRespond<IAccountRespond>>('/account/get', { params: { date_time, type }});
// 添加账目
export interface IAddAccount {
    account_type: AccountType, 
    category_id: number, 
    amount: number, 
    date_time: Date,
    notes: string
}
export const addAccountAPI = (params: IAddAccount) => axios.post<AxiosRespond<null>>('/account/add', { ...params });
// 删除账目
export const deleteAccountAPI = (account_id: number) => axios.post<AxiosRespond<null>>('/account/delete', { account_id });
// 修改账目
export interface IUpdateAccount extends IAddAccount {
    account_id: number,
}
export const updateAccountAPI = (params: IUpdateAccount) => axios.post<AxiosRespond<null>>('/account/update', { ...params });
// 根据日期或类型返回账目
export const filterAccountAPI = (limit: number, category_id?: number, date_time?: Date) => axios.post<AxiosRespond<IAccountRespond>>('/account/filter', { category_id, date_time, limit });
